const Service = require('egg').Service;

class AreaService extends Service{
    async areaInsert(name){
        const rid = this.ctx.request.headers['rid'];
        try{
            const result = await this.app.mysql.insert('area',{name:name});
            const relation = await this.app.mysql.insert('restaurant_area',{rid:rid,aid:result.insertId});
            if(result.affectedRows === 1 && relation.affectedRows === 1){
                return true;
            }else{
                return false;
            }
            console.log(result)
        }catch(error){
            return false;
        }
    }
    async getAreaList(){
        const rid = this.ctx.request.headers['rid'];
        // const result = await this.app.mysql.select('restaurant_area',{
        //     where:{rid:rid},
        //     columns:['aid']
        // });
        const result =await this.app.mysql.query('select area.* from restaurant_area inner join area on rid = ? where restaurant_area.aid = area.id',[rid])
        var string = JSON.stringify(result);
        var data = JSON.parse(string);
        return data;
    }
    async deleteArea(id){
        try{
            const result = await this.app.mysql.delete('area',{id:id});
            const relation = await this.app.mysql.delete('restaurant_area',{aid:id});
            if(result.affectedRows === 1){
                return true;
            }else{
                return false;
            }
        }catch(error){
            return false;
        }

    }
}

module.exports = AreaService;
